//
//  DAContractRWDDetailViewController.m
//  ERP
//
//  Created by goldeney on 15/2/26.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "MTCContractRWDDetailViewController.h"
#import "AutoTextTableViewCell.h"
#import "NSDictionary+DataFormat.h"

#import "NSString+handle.h"
#import "NSKeyedArchiverUserLogin.h"
#import "OMOrderDetailsViewCell.h"

#import "MTCRWDLogisticsInfoViewController.h"

@interface MTCContractRWDDetailViewController ()

@property (nonatomic,strong) NSMutableDictionary *orderInfo;
@property (nonatomic,strong) NSMutableArray *allSetArray;

@property (nonatomic,strong) NSString *orderID;
@property (nonatomic,assign) BOOL isNetworkOrder;

@end

@implementation MTCContractRWDDetailViewController
@synthesize tableView = _tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle: @"任务单详情"];
    }
    return self;
}

- (void)getPassingParameter:(id)object
{
    NSDictionary *dic = object;
    self.orderInfo = [NSMutableDictionary dictionaryWithDictionary:dic];
}

- (void)addContractInfoKey:(id)key toKey:(id)tokey defaults:(id)def format:(id)formatText
                      form:(NSDictionary*)dic toArray:(NSMutableArray*)array
{
    id value = [dic objectFormatForKey:key];
    if (!def) def = @"";
    
    if (!value){
        value = def;
    } else if (formatText) {
        value = [NSString stringWithFormat:formatText,value];
    }
    
    NSDictionary *valueDic = [NSDictionary dictionaryWithObject:value forKey:tokey];
    [array addObject:valueDic];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getOrderAllOrders:self.orderInfo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getOrderAllOrders:(NSDictionary*)dic
{
    self.allSetArray = [NSMutableArray array];
    
    NSMutableArray *orderArray = [NSMutableArray array];
    [self addContractInfoKey:@"任务单编号" toKey:@"任务单编号：" defaults:@"未知" format:nil form:dic toArray:orderArray];
    [self addContractInfoKey:@"工程名称" toKey:@"工程名称：" defaults:@"未知" format:nil form:dic toArray:orderArray];
    [self addContractInfoKey:@"站点" toKey:@"生产站点：" defaults:@"未知" format:nil form:dic toArray:orderArray];
    [self addContractInfoKey:@"施工单位" toKey:@"施工单位：" defaults:@"未知" format:nil form:dic toArray:orderArray];
    [self addContractInfoKey:@"施工部位" toKey:@"施工部位：" defaults:@"未知" format:nil form:dic toArray:orderArray];
    [self addContractInfoKey:@"前场工长" toKey:@"前场工长：" defaults:@"未知" format:nil form:dic toArray:orderArray];
    [self.allSetArray addObject:orderArray];
    
    NSMutableArray *concreteArray = [NSMutableArray array];
    [self addContractInfoKey:@"砼强度" toKey:@"强度等级：" defaults:@"未知" format:nil form:dic toArray:concreteArray];
    [self addContractInfoKey:@"坍落度" toKey:@"坍  落  度：" defaults:@"未知" format:nil form:dic toArray:concreteArray];
    [self addContractInfoKey:@"浇筑方式" toKey:@"浇筑方式：" defaults:@"未知" format:nil form:dic toArray:concreteArray];
    [self addContractInfoKey:@"计划方量" toKey:@"计划方量：" defaults:@"无" format:@"%@方" form:dic toArray:concreteArray];
    [self addContractInfoKey:@"追加方量" toKey:@"追加方量：" defaults:@"无追加" format:@"%@方" form:dic toArray:concreteArray];
    [self addContractInfoKey:@"状态" toKey:@"任务单状态：" defaults:@"未知" format:nil form:dic toArray:concreteArray];
    [self addContractInfoKey:@"工地计划开盘时间" toKey:@"计划开盘时间：" defaults:@"未知" format:nil form:dic toArray:concreteArray];
    [self addContractInfoKey:@"实际开盘时间" toKey:@"实际开盘时间：" defaults:@"未知" format:nil form:dic toArray:concreteArray];
    [self.allSetArray addObject:concreteArray];
}

#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.allSetArray.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        //发货信息
        OMOrderDetailsViewCell *cell = [[OMOrderDetailsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OMOrderDetailsViewCell"];
        cell.titleLabel.text = @"发货单信息";
        cell.iconImageView.image = [UIImage imageNamed:@"hotel_location_big"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        NSString *FCCS = [self.orderInfo objectFormatForKey:@"发车次数"];
        NSString *LJFHFL = [self.orderInfo objectFormatForKey:@"发货方量"];
        cell.subheadLabel.text = [NSString stringWithFormat:@"已发出%@车，累计发货%@方",FCCS,LJFHFL];
        return cell;
    }
    
    //通用页面
    NSMutableArray *array = [self.allSetArray objectAtIndex:indexPath.section];
    NSString *identifier = [NSString stringWithFormat:@"OrderInfoTableViewCell%ld",(long)indexPath.section];
    AutoTextTableViewCell *cell = [[AutoTextTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    [cell setDataArray:array];
    return cell;
}

#pragma mark
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, SuperWidth - 10, 29.5)];
    lable.textColor = ThemeColorBlue;
    //lable.textColor = [UIColor colorWithWhite:0.33 alpha:1.0];
    lable.font = [UIFont boldSystemFontOfSize:15];
    
    if (section  == 0) {
        lable.text = @"任务单基本信息";
    } else if(section == 1){
        lable.text = @"任务单状态";
    } else if(section == 2){
        lable.text = @"发货单信息";
    }
    [view addSubview:lable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 29.5, SuperWidth, 0.5)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
    [view addSubview:lineView];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //发货信息
    if (indexPath.section  == 2)  return 46;
    
    //通用返回
    NSMutableArray *array = [self.allSetArray objectAtIndex:indexPath.section];
    AutoTextTableViewCell *cell = [[AutoTextTableViewCell alloc] init];
    [cell setDataArray:array];
    return cell.contentView.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //物流信息
    if(indexPath.section == 2) {
        MTCRWDLogisticsInfoViewController *viewController = [[MTCRWDLogisticsInfoViewController alloc] initWithValue:self.orderInfo];
        [self.navigationController pushViewController:viewController animated:YES];
    }
}

@end
